package com.huawei.hms.auth.api.signin.internal;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.hms.activity.ForegroundIntentBuilder;
import com.huawei.hms.adapter.AvailableAdapter.AvailableCallBack;
import com.huawei.hms.auth.api.signin.HuaweiIdSignInResult;
import com.huawei.hms.auth.api.signin.internal.c.a;
import com.huawei.hms.auth.api.signin.internal.c.b;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.api.entity.hwid.HuaweiIdSignInRequest;
import com.huawei.hms.support.api.entity.hwid.HwIDNaming;
import com.huawei.hms.support.api.hwid.HuaweiIdApi;
import com.huawei.hms.support.hianalytics.HiAnalyticsClient;
import com.huawei.hms.support.log.HMSLog;
import org.json.JSONException;

public class HwIdSignInHubActivity extends Activity {
    private static boolean a = false;
    private String b = null;
    private boolean c = false;
    private HuaweiIdSignInRequest d = null;
    private HuaweiIdSignInResult e = null;

    protected void onCreate(Bundle bundle) {
        HMSLog.i("[HUAWEIIDSDK]HwIdSignInClientHub", "onCreate");
        super.onCreate(bundle);
        Intent intent = getIntent();
        if (a(intent)) {
            a(0, "invalid intent");
        }
        Object action = intent.getAction();
        if (!TextUtils.equals("com.huawei.hms.jos.signIn", action)) {
            a(0, action == null ? "unknow Action:" : "unknow Action:" + action);
        }
        CharSequence stringExtra = intent.getStringExtra("HUAWEIID_CP_CLIENTINFO");
        try {
            this.d = HuaweiIdSignInRequest.fromJson(intent.getStringExtra("HUAWEIID_SIGNIN_REQUEST"));
        } catch (JSONException e) {
            this.d = null;
            HMSLog.e("[HUAWEIIDSDK]HwIdSignInClientHub", "onCreate: JsonException");
        }
        if (TextUtils.isEmpty(stringExtra)) {
            a(0, "Activity started with invalid cp client info");
        } else if (this.d == null) {
            a(0, "Activity started with invalid sign in request info");
        } else if (bundle != null) {
            restoreInstanceState(bundle);
        } else if (a) {
            a(2013);
        } else {
            this.b = HiAnalyticsClient.reportEntry(this, HwIDNaming.signinIntent, a.a());
            a();
        }
    }

    private boolean a(Intent intent) {
        if (intent == null) {
            return true;
        }
        try {
            intent.getStringExtra("ANYTHING");
            return false;
        } catch (Exception e) {
            return true;
        }
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        int i3 = 8;
        if (!this.c && i == 16587) {
            setSignningIn(false);
            if (intent != null) {
                String stringExtra = intent.getStringExtra(HuaweiIdApi.EXTRA_SIGN_IN_ACCOUNT);
                if (stringExtra != null) {
                    try {
                        this.e = new HuaweiIdSignInResult().fromJson(stringExtra);
                        com.huawei.hms.auth.api.signin.internal.b.a.a().a(this.e.getSignInAccount(), this.d.getHuaweiIdSignInOptions());
                        i3 = this.e.getStatus().getStatusCode();
                    } catch (JSONException e) {
                        this.e = null;
                        HMSLog.e("[HUAWEIIDSDK]HwIdSignInClientHub", "onActivityResult: JsonException");
                    }
                    if (this.b != null) {
                        HiAnalyticsClient.reportExit(this, HwIDNaming.signinIntent, this.b, b.a(i3), i3, a.a());
                    }
                    setResult(i2, intent);
                    finish();
                    return;
                }
            }
            if (i2 <= 0) {
                i2 = i3;
            }
            a(i2);
        }
    }

    protected void restoreInstanceState(Bundle bundle) {
        this.b = bundle.getString("HiAnalyticsTransId");
    }

    protected void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        if (this.b != null) {
            bundle.putString("HiAnalyticsTransId", this.b);
        }
    }

    private void a(int i, String str) {
        HMSLog.e("[HUAWEIIDSDK]HwIdSignInClientHub", str);
        setResult(0);
        finish();
    }

    private final void a(int i) {
        if (this.b != null) {
            HiAnalyticsClient.reportExit(this, HwIDNaming.signinIntent, this.b, b.a(i), i, a.a());
        }
        Status status = new Status(i);
        HuaweiIdSignInResult huaweiIdSignInResult = new HuaweiIdSignInResult();
        huaweiIdSignInResult.setStatus(status);
        Intent intent = new Intent();
        try {
            intent.putExtra(HuaweiIdApi.EXTRA_SIGN_IN_ACCOUNT, huaweiIdSignInResult.toJson());
        } catch (JSONException e) {
            HMSLog.e("[HUAWEIIDSDK]HwIdSignInClientHub", "convert result to json failed");
        }
        setResult(0, intent);
        finish();
        setSignningIn(false);
    }

    private void a() {
        h.a(this, new AvailableCallBack(this) {
            final /* synthetic */ HwIdSignInHubActivity a;

            {
                this.a = r1;
            }

            public void onComplete(int i) {
                if (i == 0) {
                    this.a.b();
                    return;
                }
                HMSLog.i("[HUAWEIIDSDK]HwIdSignInClientHub", "version check failed");
                this.a.a(i);
            }
        });
    }

    private void b() {
        HMSLog.i("[HUAWEIIDSDK]HwIdSignInClientHub", "startSignInActivity");
        Intent c = c();
        setSignningIn(true);
        try {
            startActivityForResult(c, 16587);
        } catch (ActivityNotFoundException e) {
            this.c = true;
            setSignningIn(false);
            HMSLog.e("[HUAWEIIDSDK]HwIdSignInClientHub", "Launch sign in Intent failed. hms is probably being updated：" + e.getClass().getSimpleName());
            a(17);
        }
    }

    private Intent c() {
        Intent intent = getIntent();
        String stringExtra = intent.getStringExtra("HUAWEIID_CP_CLIENTINFO");
        String stringExtra2 = intent.getStringExtra("HUAWEIID_SIGNIN_REQUEST");
        String action = intent.getAction();
        try {
            a d = a.d(stringExtra);
            ForegroundIntentBuilder kitSdkVersion = new ForegroundIntentBuilder(this).setAction(action).setRequestBody(stringExtra2).setServiceName("hwid").setTransactionId(this.b).setKitSdkVersion(a.a());
            if (d.a() != null) {
                kitSdkVersion.setSubAppId(d.a());
            }
            intent = kitSdkVersion.build();
            intent.setPackage("com.huawei.hwid");
            return intent;
        } catch (Exception e) {
            HMSLog.e("[HUAWEIIDSDK]HwIdSignInClientHub", "getSignInIntent failed because:" + e.getClass().getSimpleName());
            return new Intent();
        }
    }

    protected static void setSignningIn(boolean z) {
        a = z;
    }
}
